package com.cui.myadapterdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

/**
 * @author 崔健伟
 * @Title：ViewHolder.java
 * @Date 2016.07.13 14:50
 * @description 万能ViewHolder，内容可扩充
 * Created by 崔健伟 on 2016/7/13.
 */
public class ViewHolder {
    private final SparseArray<View> mViews;
    private View mConvertView;
    private int mPosition;
    private DisplayImageOptions options;

    private ViewHolder(Context context, ViewGroup parent, int layoutId,
                       int position) {
        this.mPosition = position;
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
                false);
        //setTag
        mConvertView.setTag(this);


    }

    /**
     * 拿到一个ViewHolder对象
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static ViewHolder get(Context context, View convertView,
                                 ViewGroup parent, int layoutId, int position) {

        if (convertView == null) {
            return new ViewHolder(context, parent, layoutId, position);
        }
        return (ViewHolder) convertView.getTag();
    }


    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {

        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public View getConvertView() {
        return mConvertView;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 设置TextView的字体颜色
     *
     * @param viewId
     * @param colorId
     * @return
     */
    public ViewHolder setTextColor(int viewId, int colorId) {
        TextView view = getView(viewId);
        view.setTextColor(colorId);
        return this;
    }

    /**
     * 设置控件的显示和隐藏
     *
     * @param viewId
     * @param show
     * @return
     */
    public ViewHolder setViewShowOrGone(int viewId, int show) {
        View view = getView(viewId);
        view.setVisibility(show);
        return this;
    }

    /**
     * 设置控件的图片背景
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public ViewHolder setViewBackgroundForDrawableId(int viewId, int drawableId) {
        View view = getView(viewId);
        view.setBackgroundResource(viewId);
        return this;
    }

    /**
     * 设置控件的颜色背景
     *
     * @param viewId
     * @param colorId
     * @return
     */
    public ViewHolder setViewBackgroundForColor(int viewId, int colorId) {
        View view = getView(viewId);
        view.setBackgroundColor(colorId);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public ViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param bm
     * @return
     */
    public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param url
     * @param loadingImg
     * @param errorImg
     * @return
     */
    public ViewHolder setImageByUrlWithLoadAndError(int viewId, String url, int loadingImg, int errorImg) {
        options = new DisplayImageOptions.Builder()
                .resetViewBeforeLoading(true)
                .cacheInMemory(true)
                .showImageOnLoading(loadingImg)
                .showImageForEmptyUri(errorImg)
                .showImageOnFail(errorImg)
                .cacheOnDisc()
                .imageScaleType(ImageScaleType.EXACTLY)
                .build();
        ImageLoader.getInstance().displayImage(url, (ImageView) getView(viewId), options, null);
        return this;
    }

    /**
     * 有加载中图片
     *
     * @param viewId
     * @param url
     * @param loadingImg
     * @return
     */
    public ViewHolder setImgByUrlWithLoading(int viewId, String url, int loadingImg) {
        options = new DisplayImageOptions.Builder()
                .resetViewBeforeLoading(true)
                .cacheInMemory(true)
                .showImageOnLoading(loadingImg)
                .showImageForEmptyUri(loadingImg)
                .cacheOnDisc()
                .imageScaleType(ImageScaleType.EXACTLY)
                .build();
        ImageLoader.getInstance().displayImage(url, (ImageView) getView(viewId), options, null);
        return this;
    }

    /**
     * 有加载失败图片
     *
     * @param viewId
     * @param url
     * @param errorImg
     * @return
     */
    public ViewHolder setImgByUrlWithError(int viewId, String url, int errorImg) {
        options = new DisplayImageOptions.Builder()
                .resetViewBeforeLoading(true)
                .cacheInMemory(true)
                .showImageOnFail(errorImg)
                .showImageForEmptyUri(errorImg)
                .cacheOnDisc()
                .imageScaleType(ImageScaleType.EXACTLY)
                .build();
        ImageLoader.getInstance().displayImage(url, (ImageView) getView(viewId), options, null);
        return this;
    }

    /**
     * 加载网络图片
     *
     * @param viewId
     * @param url
     * @return
     */
    public ViewHolder setImgByUrl(int viewId, String url) {
        options = new DisplayImageOptions.Builder()
                .resetViewBeforeLoading(true)
                .cacheInMemory(true)
                .cacheOnDisc()
                .imageScaleType(ImageScaleType.EXACTLY)
                .build();
        ImageLoader.getInstance().displayImage(url, (ImageView) getView(viewId), options, null);
        return this;
    }

    public int getPosition() {
        return mPosition;
    }
}
